'''
* This is the projet for Brtc LlmOps Platform
* @Author Leon-liao <liaosiliang@alltman.com>
* @Description //TODO 
* @File: api_tool_handler.py
* @Time: 2025/9/23
* @All Rights Reserve By Brtc
'''
import uuid
from dataclasses import dataclass
from flask import request
from flask_login import login_required, current_user
from injector import inject
from internal.schema.api_tool_schema import (ValidateOpenAPISchema,
                                             CreateApiToolReq,
                                             GetApiProviderResp,
                                             GetApiToolResp,
                                             GetApiToolProvidersWithPageReq,
                                             GetApiToolProvidersWithPageResp,
                                             UpdateApiToolProviderReq
                                             )
from internal.service import ApiToolService
from pkg.paginator.paginator import PageModel
from pkg.response import validate_error_json, success_message, success_json


@inject
@dataclass
class ApiToolHandler:
    api_tool_service: ApiToolService

    @login_required
    def update_api_tool_provider(self, provider_id:uuid.UUID):
        """更新自定义API工具提供者(思路是先删除后增加)"""
        req = UpdateApiToolProviderReq()
        if not req.validate():
            return validate_error_json(req.errors)
        self.api_tool_service.update_api_tool_provider(provider_id, req, current_user)
        return success_message("更新自定义工具成功！！")

    @login_required
    def get_api_tool_providers_with_page(self):
        """获取API工具提供者分页列表信息"""
        req = GetApiToolProvidersWithPageReq(request.args)
        if not req.validate():
            return validate_error_json(req.errors)
        api_tool_providers, paginator = self.api_tool_service.get_api_tool_providers_with_page(req, current_user)
        #Many === Ture代表返回就是分页数据
        resp = GetApiToolProvidersWithPageResp(many=True)

        return success_json(PageModel(list=resp.dump(api_tool_providers), paginator= paginator))


    @login_required
    def delete_api_provider(self, provider_id:uuid.UUID):
        """根据传递的provider_id 删除对应的 工具提供商"""
        self.api_tool_service.delete_api_tool_provider(provider_id, current_user)
        return success_message("删除API工具提供商成功！")


    @login_required
    def get_api_tool(self, provider_id:uuid.UUID, tool_name:str):
        """根据传递的 provider_id + tool_id 获取工具详情信息"""
        api_tool = self.api_tool_service.get_api_tool(provider_id, tool_name)
        resp = GetApiToolResp()
        return success_json(resp.dump(api_tool))


    @login_required
    def create_api_tool_provider(self):
        """创建自定义API工具"""
        req = CreateApiToolReq()
        if not req.validate():
            return validate_error_json(req.errors)
        self.api_tool_service.create_api_tool(req, current_user)
        return success_message("创建自定义api插件成功！！")


    @login_required
    def get_api_tool_provider(self, provider_id:uuid.UUID):
        """根据传递的provider_id 获取工具提供者的原始信息"""
        api_tool_provider = self.api_tool_service.get_api_tool_provider(provider_id)
        resp = GetApiProviderResp()
        return success_json(resp.dump(api_tool_provider))


    @login_required
    def validate_openapi_schema(self):
        """校验传递的openapi_schema字符串是否正确"""
        req =  ValidateOpenAPISchema()
        if not req.validate():
            return validate_error_json(req.errors)
        self.api_tool_service.parse_openapi_schema(req.openapi_schema.data)
        return success_message("数据校验成功!!")

